package com.blog.common;

/**
 * @author: chengqiang
 * @date: 2018/12/6 13:03
 */

import com.blog.common.constant.ResponseCode;

import java.io.Serializable;

/**
 * 自定义返回响应对象
 */
public class ServiceResponse<T> implements Serializable {

    /**
     * 状态值 status
     */
    private int status;
    private String message;
    private T data;

    public ServiceResponse(int status) {
        this.status = status;
    }

    public ServiceResponse(int status, T data) {
        this.status = status;
        this.data = data;
    }

    public ServiceResponse(int status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public ServiceResponse(int status, String message) {
        this.status = status;
        this.message = message;
    }

    public int getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    public static <T> ServiceResponse<T> createBySuccess() {
        return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServiceResponse<T> createBySuccessMessage(String message) {
        return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(), message);
    }

    public static <T> ServiceResponse<T> createBySuccess(T data) {
        return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> ServiceResponse<T> createBySuccess(String message, T data) {
        return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(), message, data);
    }

    public static <T> ServiceResponse<T> createByError() {
        return new ServiceResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMessage());
    }

    public static <T> ServiceResponse<T> createByError(T data) {
        return new ServiceResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMessage(), data);
    }

    public static <T> ServiceResponse<T> createByError(ResponseCode responseCode) {
        return new ServiceResponse<T>(responseCode.getCode(),responseCode.getMessage());
    }

    public static <T> ServiceResponse<T> createByErrorMessage(String errorMessage) {
        return new ServiceResponse<T>(ResponseCode.ERROR.getCode(), errorMessage);
    }

    public static <T> ServiceResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
        return new ServiceResponse<T>(errorCode, errorMessage);
    }
}
